using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class DoanhNghiep_DAL
    {
        private SqlConnection cn = null;
        public  DoanhNghiep_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(DoanhNghiep_DTO doanhNghiep)
        {
            SqlCommand cm = new SqlCommand("tb_DOANHNGHIEP_INSERT", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDN", doanhNghiep.TenDN);
            cm.Parameters.AddWithValue("@MatKhau", doanhNghiep.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", doanhNghiep.DiaChi);
            cm.Parameters.AddWithValue("@Email", doanhNghiep.Email);
            cm.Parameters.AddWithValue("@Fax", doanhNghiep.Fax);
            cm.Parameters.AddWithValue("@DienThoai", doanhNghiep.DienThoai);
            cm.Parameters.AddWithValue("@BanDo", doanhNghiep.BanDo);
            cm.Parameters.AddWithValue("@TaiKhoan", doanhNghiep.TaiKhoan);
            cm.ExecuteNonQuery();
        }
        public void Update(DoanhNghiep_DTO doanhNghiep)
        {
            SqlCommand cm = new SqlCommand("tb_DoanhNghiep_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", doanhNghiep.MaDN);
            cm.Parameters.AddWithValue("@TenDN", doanhNghiep.TenDN);
            cm.Parameters.AddWithValue("@MatKhau", doanhNghiep.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", doanhNghiep.DiaChi);
            cm.Parameters.AddWithValue("@Email", doanhNghiep.Email);
            cm.Parameters.AddWithValue("@FAX", doanhNghiep.Fax);
            cm.Parameters.AddWithValue("@DienThoai", doanhNghiep.DienThoai);
            cm.Parameters.AddWithValue("@BanDo", doanhNghiep.BanDo);
            cm.Parameters.AddWithValue("@TaiKhoan", doanhNghiep.TaiKhoan);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tb_DoanhNghiep_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", iD);
            cm.ExecuteNonQuery();
        }
        public tbDoanhNghiepCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tb_DoanhNghiep_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
        
            SqlDataReader dr = cm.ExecuteReader();
            tbDoanhNghiepCollection doanhNghiepCollection = new tbDoanhNghiepCollection();

            while (dr.Read())
            {
                DoanhNghiep_DTO doanhNghiep = new DoanhNghiep_DTO();
                doanhNghiep.MaDN = int.Parse(dr.GetValue(0).ToString());
                doanhNghiep.TenDN = dr.GetValue(1).ToString();
                doanhNghiep.MatKhau = dr.GetValue(2).ToString();
                doanhNghiep.DiaChi = dr.GetValue(3).ToString();
                doanhNghiep.Email = dr.GetValue(4).ToString();
                doanhNghiep.Fax = dr.GetValue(5).ToString();
                doanhNghiep.DienThoai = dr.GetValue(6).ToString();
                doanhNghiep.BanDo = dr.GetValue(7).ToString();
                doanhNghiep.TaiKhoan = float.Parse(dr.GetValue(8).ToString());
                doanhNghiepCollection.Add(doanhNghiep);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return doanhNghiepCollection;
        }
    }
}
